<?php

namespace app\backend\controllers;

use app\backend\common\AdminBaseController;
use app\backend\models\ArticleClassModel;
use app\backend\models\BookkeepingModel;
use app\backend\models\UserModel;
use lib\db\DbTool;
use lib\helper\error;
use lib\Tool\Tools;

class BookkeepingController extends AdminBaseController
{
    public function actionList(){
        $BookkeepingModel = new BookkeepingModel();
        $data = $BookkeepingModel->getBookKeepingAll();
        $this->assign('data', ['bookkeeping' => $data]);
        return $this->display('bookkeeping/list');
    }

    public function actionAdd(){
        $post = Tools::My()->request()->post();
        if (!empty($post)){
            $code = BookkeepingModel::find()->insert([
                'b_name'=>$post['b_name'],
                'consume_money'=>$post['consume_money'],
                'add_time'=>time(),
                'is_del'=>0,
                'b_type'=>$post['b_type'],
                'user_id'=>self::$UserInfo['user_id']
            ]);
            if ($code > 0){
                error::_plainError(['message'=>'记录成功']);
            }
            error::_plainError(['message'=>'记录失败']);
        }
        return $this->display('bookkeeping/add');
    }

    public function actionPerCapita(){
        $BookkeepingModel = new BookkeepingModel();
        $data = $BookkeepingModel->getBookKeepingAll();
        $payData = [];
        $incomeData = [];
        $timeData = [];
        $userData =[];
        $UserModel = new UserModel();
        $userList = $UserModel->getUser([['=','is_bill',1]],'user_id ASC');
        $userList = array_column($userList,null,'user_id');
        foreach ($userList as $k=>$v){
            $userData[$k][99999] =[
              'real_name'=>$v['real_name'],
              'consume_money'=>0,
              'b_type'=>1
            ];
        }
        foreach ($data as $k=>$v){
            if ($v['b_type']==1){
                $payData[] =$v;
                $timeData[date('Y-m-d',$v['add_time'])][]=$v;
                $userData[$v['user_id']][] = $v;
            }else{
                $incomeData[] =$v;
                $timeData1[date('Y-m-d',$v['add_time'])][]=$v;
            }
        }
        $this->assign('data', ['pay' => $payData,'income'=>$incomeData,'times'=>$timeData,'user'=>$userData]);
        return $this->display('bookkeeping/perCapita');
    }
}